package com.EAsystem.service.Impl;

import com.EAsystem.entity.Attendance;
import com.EAsystem.mapper.AttendanceMapper;
import com.EAsystem.service.AttendanceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考勤管理Service实现类
 */
@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;
    
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public boolean addAttendance(Attendance attendance) {
        // 设置创建和更新时间
        String now = sdf.format(new Date());
        attendance.setCreateTime(now);
        attendance.setUpdateTime(now);
        return attendanceMapper.insertAttendance(attendance) > 0;
    }



    @Override
    public boolean updateAttendance(Attendance attendance) {
        // 设置更新时间
        attendance.setUpdateTime(sdf.format(new Date()));
        return attendanceMapper.updateAttendance(attendance) > 0;
    }

    @Override
    public boolean deleteAttendance(Long id) {
        return attendanceMapper.deleteAttendanceById(id) > 0;
    }

    @Override
    public Attendance getAttendanceById(Long id) {
        return attendanceMapper.findAttendanceById(id);
    }



    @Override
    public List<Attendance> getAttendancesByStudentId(Integer studentId) {
        return attendanceMapper.findAttendancesByStudentId(studentId);
    }






    @Override
    public PageInfo<Attendance> getAllAttendancesWithPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Attendance> attendances = attendanceMapper.findAllAttendancesWithPage();
        return new PageInfo<>(attendances);
    }

    @Override
    public PageInfo<Attendance> getAttendancesByConditionWithPage(Attendance attendance, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Attendance> attendances = attendanceMapper.findAttendancesByCondition(attendance);
        return new PageInfo<>(attendances);
    }

    @Override
    public Map<String, Integer> countAttendanceStatusByStudentIdAndCourseId(Integer studentId, Integer courseId) {
        return attendanceMapper.countAttendanceStatusByStudentIdAndCourseId(studentId, courseId);
    }


}